<?php

namespace App\Http\Requests\Api;

class PermissionRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        switch ($this->route()->getName()) {
            case 'permissions.store':
                return [
                    'name' => 'required|min:3|max:60|unique:permissions',
                    'label' => 'sometimes|max:20'
                ];

            case 'permissions.update':
                $id = $this->segment(4); // 排除掉当前权限
                return [
                    'name' => 'sometimes|min:3|max:60|unique:permissions,name,'.$id,
                    'label' => 'sometimes|max:20'
                ];

            default:;
        }
    }

    public function messages(): array
    {
        return [
            'name.required' => '权限标识不能为空',
            'name.min' => '权限标识最小长度为3个字符',
            'name.max' => '权限标识最大长度为60个字符',
            'name.unique' => '权限标识已经存在',
            'label.max' => '权限名最大长度为20个字符',
        ];
    }
}
